//
//  IARecordViewController.m
//  VoiceClass
//
//  Created by caoxiaobo on 2018/1/1.
//  Copyright © 2018年 YRX. All rights reserved.
//

#import "IARecordViewController.h"
#import "IAVoiceLoadingView.h"
#import "MBProgressHUD.h"
#import "Definition.h"

CGFloat kLabelButtonH = 60.0f;

@interface IARecordViewController ()

@property (nonatomic, strong) IAVoiceLoadingView *voiceLoadingView;

@property (strong, nonatomic, readwrite) UILabel *answerLabelButton;
@property (nonatomic, strong, readwrite) IFlySpeechRecognizer *speechRecognizer;

@end

@implementation IARecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.voiceLoadingView];
    self.voiceLoadingView.hidden = YES;
//    [self setupSubViews];
    
    self.answerLabelButton = [[UILabel alloc] init];
    
    CGFloat selfHeight = self.view.cHeight;
    CGFloat selfWidth = self.view.cWidth;
    self.answerLabelButton.frame = CGRectMake(0, selfHeight - kLabelButtonH, selfWidth, kLabelButtonH);
    [self.view addSubview:self.answerLabelButton];
    
    self.answerLabelButton.backgroundColor = HEXCOLOR(0x42A8FF);
    self.answerLabelButton.font = [UIFont boldSystemFontOfSize:20.0f];
    self.answerLabelButton.textColor = [UIColor whiteColor];
    self.answerLabelButton.textAlignment = NSTextAlignmentCenter;
    self.answerLabelButton.userInteractionEnabled = YES;
    UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [self.answerLabelButton addGestureRecognizer:longGesture];
}

- (void)viewWillDisappear:(BOOL)animated {
    [_speechRecognizer cancel]; //取消识别
    [_speechRecognizer setDelegate:nil];
    [_speechRecognizer setParameter:@"" forKey:[IFlySpeechConstant PARAMS]];
}

- (void)viewWillAppear:(BOOL)animated {
    [self initRecognizer];
}

- (void)setupSubViews {
    
}

- (void)longPress:(UILongPressGestureRecognizer *)gesture {
}

- (void)startRecord {
    
    if (self.speechRecognizer == nil) {
        [self initRecognizer];
    }
    [self.speechRecognizer cancel];
    //设置音频来源为麦克风
    [self.speechRecognizer setParameter:IFLY_AUDIO_SOURCE_MIC forKey:@"audio_source"];
    //设置听写结果格式为json
    [self.speechRecognizer setParameter:@"json" forKey:[IFlySpeechConstant RESULT_TYPE]];
    //保存录音文件，保存在sdk工作路径中，如未设置工作路径，则默认保存在library/cache下
    [self.speechRecognizer setParameter:@"asr.pcm" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];
    [self.speechRecognizer setDelegate:self];
    
    BOOL ret = [self.speechRecognizer startListening];
    if (!ret) {
        // 弹框提示
        [self showErrorHud:@"语言听写初始化失败, 请重试!"];
    }
    
    self.voiceLoadingView.hidden = NO;
    [self.view bringSubviewToFront:self.voiceLoadingView];
}

- (void)stopRecord {
    [self.speechRecognizer stopListening];
    self.voiceLoadingView.hidden = YES;
}

- (void)showErrorHud:(NSString *)msg {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = msg;
    hud.mode = MBProgressHUDModeText;
    [hud hideAnimated:YES afterDelay:1.0];
}

-(void)initRecognizer
{
    //单例模式，无UI的实例
    if (_speechRecognizer == nil) {
        _speechRecognizer = [IFlySpeechRecognizer sharedInstance];
        [_speechRecognizer setParameter:@"" forKey:[IFlySpeechConstant PARAMS]];
        //设置听写模式
        [_speechRecognizer setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
    }
    _speechRecognizer.delegate = self;
    if (_speechRecognizer != nil) {
        IATConfig *instance = [IATConfig sharedInstance];
        //设置最长录音时间
        [_speechRecognizer setParameter:instance.speechTimeout forKey:[IFlySpeechConstant SPEECH_TIMEOUT]];
        //设置后端点
        [_speechRecognizer setParameter:instance.vadEos forKey:[IFlySpeechConstant VAD_EOS]];
        //设置前端点
        [_speechRecognizer setParameter:instance.vadBos forKey:[IFlySpeechConstant VAD_BOS]];
        //网络等待时间
        [_speechRecognizer setParameter:@"20000" forKey:[IFlySpeechConstant NET_TIMEOUT]];
        //设置采样率，推荐使用16K
        [_speechRecognizer setParameter:instance.sampleRate forKey:[IFlySpeechConstant SAMPLE_RATE]];
        //设置语言
        [_speechRecognizer setParameter:instance.language forKey:[IFlySpeechConstant LANGUAGE]];
        //设置方言
        [_speechRecognizer setParameter:instance.accent forKey:[IFlySpeechConstant ACCENT]];
        //设置是否返回标点符号
        [_speechRecognizer setParameter:instance.dot forKey:[IFlySpeechConstant ASR_PTT]];
    }
}

- (IAVoiceLoadingView *)voiceLoadingView {
    if (!_voiceLoadingView) {
        _voiceLoadingView = [[IAVoiceLoadingView alloc] init];
        CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
        CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
        CGFloat y = 64.0f;
        CGFloat h = screenH - y - kLabelButtonH;
        _voiceLoadingView.frame = CGRectMake(0, y, screenW, h);
        _voiceLoadingView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.4];
    }
    return _voiceLoadingView;
}

#pragma mark - IFlySpeechRecognizerDelegate

- (void)onError:(IFlySpeechError *) errorCode {
}

- (void)onResults:(NSArray *) results isLast:(BOOL)isLast {
}

@end
